//
//  ATDrawerViewController.m
//  Adorable_Things
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "ATDrawerViewController.h"
#import "ATMyFavoriteViewController.h"
#import "ATMsgCenterViewController.h"
#import "ATRootTabBarController.h"
#import "ATSetViewController.h"
#import "ATDrawerTableViewCell.h"
#import "UIViewController+JDSideMenu.h"
#import "ATLoginViewController.h"

@interface ATDrawerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableV;

@property (nonatomic, assign) NSInteger index;

@end

@implementation ATDrawerViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor blackColor];
    
    self.index = 10;
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    self.tableV.backgroundColor = [UIColor blackColor];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ATDrawerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DrawerCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ATDrawerTableViewCell" owner:self options:nil] lastObject] ;
    }
   NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:kUserInfor];
   ATUserManager *manager = [ATUserManager shareManager];
    manager.avatar = dic[@"avatar"];
    manager.nickname = dic[@"nickname"];
    switch (indexPath.row) {
        case 0:
            
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:manager.avatar] placeholderImage:[UIImage imageNamed:@"avatar-no"]];
            if (manager.isLogin == 0) {
                cell.nameLab.text = @"未登录";
            }else{
                cell.nameLab.text = manager.nickname;
                cell.textLabel.font = [UIFont systemFontOfSize:10];
            }
            
            break;
        case 1:
            cell.imageV.image = [UIImage imageNamed:@"iconfont-cloud"];
            cell.nameLab.text = @"首页";
            break;
        case 2:
            cell.imageV.image = [UIImage imageNamed:@"iconfont-cloud"];
            cell.nameLab.text = @"我的收藏";
            break;
        case 3:
            cell.imageV.image = [UIImage imageNamed:@"iconfont-cloud"];
            cell.nameLab.text = @"设置中心";
            break;
            
        
            
        default:
            break;
    }
    
    cell.imageV.layer.cornerRadius = 10;
    cell.imageV.layer.masksToBounds = YES;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor blackColor];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHeight / 8;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:kUserInfor];
    ATUserManager *manager = [ATUserManager shareManager];
    manager.avatar = dic[@"avatar"];
    manager.nickname = dic[@"nickname"];
    if (indexPath.row != self.index) {
        if (indexPath.row == 0) {
            if (manager.isLogin == 0) {
                ATLoginViewController *loginVC = [[ATLoginViewController alloc] init];
                UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:loginVC];
                loginVC.navigationController.navigationBar.hidden = YES;
                [self presentViewController:naVC animated:YES completion:nil];
            }
        }
        if (indexPath.row == 1) {
            ATRootTabBarController *rootVC = [[ATRootTabBarController alloc]init];
            [self.sideMenuController setContentController:rootVC animted:YES];
        }
        if (indexPath.row == 2) {
            ATMyFavoriteViewController *favVC = [[ATMyFavoriteViewController alloc]init];
            UINavigationController *favNavC = [[UINavigationController alloc] initWithRootViewController:favVC];
            [self.sideMenuController setContentController:favNavC animted:YES];
        }
        
        if (indexPath.row == 3) {
            ATSetViewController *setVC = [[ATSetViewController alloc]init];
            UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:setVC];
            nav.navigationBarHidden = YES;
            
            __weak ATDrawerViewController *drawerVC = self;
            setVC.setBlock = ^void(){
                [drawerVC.tableV reloadData];
            };
            
            [self.sideMenuController setContentController:nav animted:YES];
        }
        self.index = indexPath.row;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
